<?php


namespace App\Services\Traits;


trait ServicesProviderTrait
{
    public static function getModel(string $modelName)
    {
        return str_replace('Services', 'Models', self::$namespace) . '\\' . ucfirst($modelName);
    }

    public function __get(string $serviceName)
    {
        return self::makeService($serviceName);
    }

    private static function makeService(string $serviceName)
    {
        $hash = md5($serviceName);

        if (!array_key_exists($hash, self::$services)) {

            $service = self::$namespace . '\\' . ucfirst($serviceName) . 'Service';
            if(!class_exists($service)) die('Service Not Found');

            self::$services[$hash] = new $service();
        }
        return self::$services[$hash];
    }
}
